package lumingweihua.future.cn.lumingweihua.ui.order;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zhusx.core.adapter.Lib_BaseRecyclerAdapter;
import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;
import com.zhusx.core.utils._Densitys;
import com.zhusx.core.utils._Intents;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.network.LoadData;
import lumingweihua.future.cn.lumingweihua.network.LoadingHelper;
import lumingweihua.future.cn.lumingweihua.ui.BaseAppFragment;
import lumingweihua.future.cn.lumingweihua.ui.order.domain.YunShuData;
import lumingweihua.future.cn.lumingweihua.widget.FrescoImageView;

/**
 * Created by z1986 on 2018/3/16.
 */

public class OrderGoodsInfoFragment extends BaseAppFragment {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    Lib_BaseRecyclerAdapter<YunShuData> adapter;
    public String good_num;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_order_goods_info, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);
        initView();
        LoadData<List<YunShuData>> loadData = new LoadData<>(LoadData.Api.货主订单运输信息, this);
        loadData._setOnLoadingListener(new LoadingHelper<List<YunShuData>>(recyclerView, loadData) {
            @Override
            public void __onComplete(HttpRequest<Object> request, IHttpResult<List<YunShuData>> data) {
                adapter._setItemToUpdate(data.getData());
            }
        });
        loadData._loadData(good_num);
    }

    private void initView() {
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.set(0, _Densitys.dip2px(view.getContext(), 10), 10, 10);
            }
        });
        recyclerView.setAdapter(adapter = new Lib_BaseRecyclerAdapter<YunShuData>() {
            @Override
            protected void __bindViewHolder(_ViewHolder viewHolder, int i, final YunShuData s) {
                viewHolder.setText(R.id.tv_name, s.car_name);
                viewHolder.setText(R.id.tv_car_num, s.license);
                viewHolder.setText(R.id.tv_count, "装货:" + s.rough_weight + "吨");
                viewHolder.<FrescoImageView>getView(R.id.iv_image).setImageURI(s.avatar_url);
                switch (String.valueOf(s.carriage_status)) {
                    case "1"://待装货
                        viewHolder.setText(R.id.tv_type, "待装货");
                        break;
                    case "2"://运输中
                        viewHolder.setText(R.id.tv_type, "运输中");
                        break;
                    case "3"://已结束
                        viewHolder.setText(R.id.tv_type, "已结束");
                        break;
                }
                viewHolder.setText(R.id.tv_message, s.carriage);
                viewHolder.getView(R.id.tv_call).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!TextUtils.isEmpty(s.car_mobile)) {
                            new AlertDialog.Builder(getActivity())
                                    .setMessage("是否拨打电话给司机?")
                                    .setNegativeButton("拨打", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                            _Intents.dialPhone(getActivity(), s.car_mobile);
                                        }
                                    })
                                    .setPositiveButton("取消", null)
                                    .create()
                                    .show();
                        }
                    }
                });
                viewHolder.getView(R.id.tv_message).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(view.getContext(), WuLiuActivity.class)
                                .putExtra(_EXTRA_String_ID, s.car_id)
                                .putExtra(_EXTRA_String, good_num)
                        );
                    }
                });
            }

            @Override
            protected int __getLayoutResource(int i) {
                return R.layout.item_list_order_goods;
            }
        });
    }

    public void setId(String good_num) {
        this.good_num = good_num;
    }
}
